import {
  schoolCourseLsit
} from '@/api/course/course'
import {
  adddShoppingCar
} from '@/api/carts.js'
import {
  addCourseBuy
} from '@/api/buy.js'
import {
  get_img_src
} from '@/utils/index.js'
import CourseSection from '@/components/Course/CourseSection/index.vue'
export default {
  components: {
    CourseSection
  },
  data() {
    return {
      // 已加入购物车标识
      addCartF: '加入购物车',
      stages: [],
      grades: [],
      subjects: [],
      versions: [],
      volumes: [],
      listPages: {
        layout: 'total, sizes, prev, pager, next, jumper', // 分页显示参数
        current: 1, // 当前所在分页数
        sizes: [5, 10, 15, 20], // 每页显示的条目数设置
        size: 5, // 每页显示的条目数
        total: 0, // 总条数
        count: 5, // 总页数=总条数/每页条目
        counter: 7, // 页码按钮的数量，当总页数超过该值时会折叠
        prevtext: '上一页',
        nexttext: '下一页',
        single: false
      },
      listParam: {
        sign: 3, // 标记 1:本校；2:外校；3可购买课程；4已购买课程
        learningPhaseId: '', // 学段ID
        courseGradeId: '', // 年级ID
        courseSubjectId: '', // 学科ID
        courseVersionId: '', // 版本ID
        courseVolumeId: '', // 册别ID
        page: 1, // 第N页
        size: 5 // 每页显示条数
      },
      listDatas: []
    }
  },
  created() {
    this.getCourseLsit()
  },
  watch: {
    $route: {
      handler() {
        this.listParam.learningPhaseId = this.$route.query.learningPhaseId || null
        this.listParam.courseGradeId = this.$route.query.courseGradeId || null
        this.listParam.courseSubjectId = this.$route.query.courseSubjectId || null
        this.listParam.courseVersionId = this.$route.query.courseVersionId || null
        this.listParam.courseVolumeId = this.$route.query.courseVolumeId || null
      },
      deep: true
    },
    listParam: {
      handler() {
        this.getCourseLsit()
      },
      deep: true
    }
  },
  methods: {
    getCourseLsit() { // 课程列表
      this.loadObj = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      schoolCourseLsit(this.listParam).then(response => {
        if (response.code === 20000 || response.code === 200) {
          this.listDatas = response.data
          this.listPages.total = response.total
        } else {
          this.$message({
            message: response.msg,
            type: 'warning'
          })
        }
        this.loadObj.close()
      }).catch(error => {
        this.$message.error('获取课程列表失败!!!' + error)
      })
    },
    page_size_change(size) { // 改变每页条数
      this.listParam.size = size
      this.listPages.size = size
    },
    page_current_change(page) { // 点击当前页面
      this.listParam.page = page
      this.listPages.page = page
    },
    page_prev_click() { // 点击上一页
      this.listPages.page--
      this.listParam.page = this.listPages.page
    },
    page_next_click() { // 点击下一页
      this.listPages.page++
      this.listParam.page = this.listPages.page
    },
    img_src(val) {
      return get_img_src(val)
    },
    addToCart(item) { // 加入购物车
      adddShoppingCar({
        courseId: item.courseId
      }).then(response => {
        if (response.code) {
          if (response.code === 20000 || response.code === 200) {
            this.$message({
              message: '加入购物车成功',
              type: 'success'
            })
            item.addCartF = true
          } else {
            if(response.code === 500 &&response.msg =="已加入不允许重复加入"){
              item.addCartF = true
            }
            console.log(item.addCartF)
          }
        } else if (response.data.code) {
          if (response.data.code === 20000 || response.data.code === 200) {
            this.$message({
              message: '加入购物车成功',
              type: 'success'
            })
          } else {
            if(response.data.code === 500 &&response.data.msg =="已加入不允许重复加入"){
            }
          }
        } else {
          // console.log('00')
        }
      })
    },
    clickTo(item) {
      this.$router.push({
        path: '/goods/detail',
        query: {
          courseId: item.courseId
        }
      })
    },
    buyNow(item) { // 立即购买
      location.href = process.env.VUE_APP_BASE_API + '/course/buy/addCourseBuy?courseId=' + item.courseId + '&token=' + this.$store.getters.token
      // addCourseBuy({
      //   courseId: item.courseId
      // }).then(response => {
      //   if (response.code === 20000 || response.code === 200) {
      //     this.$message({
      //       message: response.msg,
      //       type: 'success'
      //     })
      //     this.getCourseLsit()
      //   } else {
      //     this.$message({
      //       message: response.msg,
      //       type: 'warning'
      //     })
      //   }
      // })
      // .catch(error => {
      //   this.$message.error('服务器内服错误！购买失败' + error)
      // })
    }
  }
}
